/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.bean.CooperBillBean;
import com.minierp.bean.CooperBillItemBean;
import com.minierp.helper.EncCommonHelper;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class CooperBillItemListExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"CooperBillItemList.xls\"");
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints(10);
		font.setColor(10);
		font.setFontName("黑体");
		font.setBoldweight(700);
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFont(font);
		cellStyle.setAlignment(2);
		cellStyle.setBorderBottom(1);
		cellStyle.setWrapText(true);
		cellStyle.setFillForegroundColor(22);
		cellStyle.setFillPattern(1);
		HSSFSheet sheet = workbook.createSheet("list");
		List list = (List) model.get("list");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		r.createCell(11);
		r.createCell(12);
		r.createCell(13);
		r.createCell(14);
		r.createCell(15);
		String totalStr = "金额合计：" + model.get("costTotal").toString();
		totalStr = totalStr + " " + "面积合计："
				+ model.get("squareTotal").toString() + "元";
		totalStr = totalStr + " " + "长度合计："
				+ model.get("lengMTotal").toString() + "米 "
				+ model.get("lengCmTotal").toString() + "公分";
		totalStr = totalStr + " " + "合同单数：" + model.get("billTotal") + "笔";
		c.setCellValue(new HSSFRichTextString(totalStr));
		sheet.addMergedRegion(new Region(0, 0, 0, 15));
		r = sheet.createRow(1);
		r.createCell(0).setCellValue(new HSSFRichTextString("外协单号"));
		r.createCell(1).setCellValue(new HSSFRichTextString("业务单号"));
		r.createCell(2).setCellValue(new HSSFRichTextString("日期"));
		r.createCell(3).setCellValue(new HSSFRichTextString("外协单位"));
		r.createCell(4).setCellValue(new HSSFRichTextString("项目类型"));
		r.createCell(5).setCellValue(new HSSFRichTextString("内容"));
		r.createCell(6).setCellValue(new HSSFRichTextString("材质"));
		r.createCell(7).setCellValue(new HSSFRichTextString("规格"));
		r.createCell(8).setCellValue(new HSSFRichTextString("制作要求"));
		r.createCell(9).setCellValue(new HSSFRichTextString("单位"));
		r.createCell(10).setCellValue(new HSSFRichTextString("数量"));
		r.createCell(11).setCellValue(new HSSFRichTextString("单价"));
		r.createCell(12).setCellValue(new HSSFRichTextString("金额"));
		r.createCell(13).setCellValue(new HSSFRichTextString("业务员"));
		r.createCell(14).setCellValue(new HSSFRichTextString("开单人"));
		r.createCell(15).setCellValue(new HSSFRichTextString("部门"));
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			CooperBillBean cBean = (CooperBillBean) objs[0];
			String estName = (String) objs[1];
			CooperBillItemBean iBean = (CooperBillItemBean) objs[2];
			String unit = "";
			String spec = iBean.getCooperBillItem().getSpecification();
			if (iBean.getCooperBillItem().getPriceType().equals("AREA")) {
				unit = "平方米";
				spec = spec
						+ " x "
						+ EncCommonHelper.parseBigDecimal(iBean
								.getCooperBillItem().getAmount());
			} else if (iBean.getCooperBillItem().getPriceType()
					.equals("LENGTH")) {
				if (iBean.getCooperBillItem().getLengType().equals("M")) {
					unit = "米";
				} else {
					unit = "公分";
				}

				spec = spec
						+ " x "
						+ EncCommonHelper.parseBigDecimal(iBean
								.getCooperBillItem().getAmount());
			} else {
				unit = iBean.getCooperBillItem().getUnit();
			}

			r = sheet.createRow(os + 2);
			r.createCell(0)
					.setCellValue(
							new HSSFRichTextString(cBean.getCooperBill()
									.getBillCode()));
			r.createCell(1).setCellValue(
					new HSSFRichTextString(iBean.getCooperBillItem()
							.getBbCode()));
			r.createCell(2).setCellValue(
					new HSSFRichTextString(sf.format(cBean.getCooperBill()
							.getCooperDate())));
			r.createCell(3).setCellValue(new HSSFRichTextString(estName));
			r.createCell(4).setCellValue(
					new HSSFRichTextString(iBean.getMakeTypeName()));
			r.createCell(5).setCellValue(
					new HSSFRichTextString(iBean.getCooperBillItem()
							.getFinishedContent()));
			r.createCell(6).setCellValue(
					new HSSFRichTextString(iBean.getCooperBillItem()
							.getProName()));
			r.createCell(7).setCellValue(new HSSFRichTextString(spec));
			r.createCell(8).setCellValue(
					new HSSFRichTextString(iBean.getCooperBillItem()
							.getMakeRequire()));
			r.createCell(9).setCellValue(new HSSFRichTextString(unit));
			r.createCell(10).setCellValue(
					iBean.getCooperBillItem().getAmount().doubleValue());
			r.createCell(11).setCellValue(
					iBean.getCooperBillItem().getUnitPrice().doubleValue());
			r.createCell(12).setCellValue(
					iBean.getCooperBillItem().getCost().doubleValue());
			r.createCell(13).setCellValue(
					new HSSFRichTextString(cBean.getCooperBill()
							.getManagerName()));
			r.createCell(14).setCellValue(
					new HSSFRichTextString(cBean.getCooperBill()
							.getCreaterName()));
			r.createCell(15).setCellValue(
					new HSSFRichTextString(cBean.getDepartmentName()));
		}

		ServletOutputStream arg19 = response.getOutputStream();
		workbook.write(arg19);
		arg19.flush();
		arg19.close();
	}
}